package cn.akwangl.common;

import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;

/**
 * Http 请求统一响应包装类.
 *
 * @author youyongkun
 * @date 2022/5/17 13:43
 * @since 0.0.2
 */
public class Result<T> {

    private static final long ELEMENT_SIZE = 100;

    /**
     * 响应业务状态
     */
    private Integer status;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应中的数据
     */
    private T data;

    public Result(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public Result(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public static <T> Result<T> createResult(Integer status, String msg, T data) {
        return new Result(status, msg, data);
    }

    public static <T> Result<T> success() {
        return Result.success(null);
    }

    public static <T> Result<T> success(T data) {
        return Result.createResult(200, "操作成功", data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return Result.createResult(200, msg, data);
    }

    public static Result errorMsg(String msg) {
        return Result.createResult(500, msg, null);
    }


    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    @SneakyThrows
    @Override
    public String toString() {
        String dataStr = "响应数据太大已忽略";
        if (this.data != null) {
            if (data instanceof Collection) {
                Collection c = (Collection) data;
                if (!c.isEmpty() && c.size() < ELEMENT_SIZE) {
                    dataStr = JSONObject.toJSONString(this);
                }
            } else if (data instanceof Map) {
                Map m = (Map) data;
                if (!m.isEmpty() && m.size() < ELEMENT_SIZE) {
                    dataStr = JSONObject.toJSONString(this);
                }
            } else {
                Class<?> dataClazz = this.data.getClass();
                Field[] fields = dataClazz.getDeclaredFields();

                for (Field field : fields) {
                    field.setAccessible(true);
                    Object fieldValue = field.get(data);
                    if (fieldValue != null) {
                        if (field.getType().isAssignableFrom(Collection.class)) {
                            Collection c = (Collection) fieldValue;
                            if (!c.isEmpty() && c.size() < ELEMENT_SIZE) {
                                dataStr = JSONObject.toJSONString(this);
                                break;
                            }
                        } else if (field.getType().isAssignableFrom(Map.class)) {
                            Map m = (Map) fieldValue;
                            if (!m.isEmpty() && m.size() < ELEMENT_SIZE) {
                                dataStr = JSONObject.toJSONString(this);
                                break;
                            }
                        }
                    }
                }
            }
        }
        return dataStr;
    }
}
